package c.x.jy.service.utils;

import java.util.List;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.beanutils.PropertyUtils;

import love.cq.util.StringUtil;

 

public class AliOSSImgProcessUtil {
/**
 * 图片处理 1、更改图片清晰度 
2、缩小图片尺寸 
 * @param obj
 * @param fields
 */
    public static void process(Object obj,String... fields){
        for(String field:fields) {
            process(obj,field,null,null);
        }
    }

    public static void process(Object obj,Integer width,Integer height,String... fields){
        for(String field:fields) {
            process(obj,field,width,height);
        }
    }


    public static void processArray(Object obj,Integer width,Integer height,String fields){
        String  o =null;
        try{
            o = (String) PropertyUtils.getProperty(obj, fields);
        }catch(Exception e){

        }
        if(o==null)try{
            o = (String)  MethodUtils.invokeMethod(obj, "get", fields);
        }catch(Exception e) {

        }
        if(obj!=null&&o!=null)
        processArray( obj, width, height, fields, o.split("#"));
    }



    public static <T> void process(List<T> list, String... field){
        if(list!=null){
            for(T t:list){
                process(t,field);
            }
        }
    }

    public static <T> void process(List<T> list,Integer width,Integer height, String... field){
        if(list!=null){
            for(T t:list){
                process(t,width,height,field);
            }
        }
    }

    /**
     * 处理图片
     * @param obj
     * @param field
     * @param width 图片宽
     * @param height 图片高度
     */
    public static void process(Object obj,String field,Integer width,Integer height){
        String o = null;
        try {
            o = (String) PropertyUtils.getProperty(obj, field);
            if (!StringUtil.isBlank (o) && !o.startsWith("http")&&isImage(o)) {
                String value = Configuration.OssFileUrl + o;
                if(width!=null && height!=null){
                    value += "?x-oss-process=image/resize,m_fill,h_"+height+",w_"+width;
                }
                PropertyUtils.setProperty(obj, field, value);
            }
        } catch (Exception e) {
            e.getMessage() ;
        }
 
        try {
            if (o == null) {
                o = (String) MethodUtils.invokeMethod(obj, "get", field);
                if (!StringUtil.isBlank(o) && !o.startsWith("http")&&isImage(o)) {
                    String value = Configuration.OssFileUrl + o;
                    if(width!=null && height!=null){
                        value += "?x-oss-process=image/resize,m_fill,h_"+height+",w_"+width;
                    }
                    MethodUtils.invokeMethod(obj, "put", new Object[]{field, value});
                }
            }
        } catch (Exception e) {
             e.getMessage() ;
        }
    }

    
    private static boolean isImage(String name){
	
    	//BMP、JPG、JPEG、PNG、GIF
    	return (name!=null&&(name.toLowerCase().endsWith(".jpg")||name.toLowerCase().endsWith(".bmp")
    			||name.toLowerCase().endsWith(".jpeg")||name.toLowerCase().endsWith(".png")
    			||name.toLowerCase().endsWith(".gif")));    	
    }

    /**
     * 处理图片
     * @param obj
     * @param field
     * @param width 图片宽
     * @param height 图片高度
     */
    public static void processArray(Object obj,Integer width,Integer height,String field,String... images){
            String o = null;
        try {
            o = (String) PropertyUtils.getProperty(obj, field);
            String value ="";
            for(String s :images){
                if (!StringUtil.isBlank(s) && !s.startsWith("http")&&isImage(o)) {
                    value=(value.equals("")?"":value+" #")+ Configuration.OssFileUrl + s;
                    if(width!=null && height!=null){
                        value += "?x-oss-process=image/resize,m_fill,h_"+height+",w_"+width;
                    }

                }
            }
            PropertyUtils.setProperty(obj, field, value);
        } catch (Exception e) {
           e.getMessage() ;
        }

        try {
            if (o == null) {
                o = (String) MethodUtils.invokeMethod(obj, "get", field);
                String value ="";
                for(String s :images){
                    if (!StringUtil.isBlank(s) && !s.startsWith("http")&&isImage(o)) {
                        value=(value.equals("")?"":value+" #")+ Configuration.OssFileUrl + s;
                        if(width!=null && height!=null){
                            value += "?x-oss-process=image/resize,m_fill,h_"+height+",w_"+width;
                        }

                    }
                }
                    MethodUtils.invokeMethod(obj, "put", new Object[]{field, value});

            }
        } catch (Exception e) {
            e.getMessage() ;
        }
    }
}
